package p.ithorns.integration.idempotent;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.*;
import p.ithorns.framework.idempotent.EnableIdempotent;
import p.ithorns.framework.idempotent.Idempotent;

import javax.annotation.Resource;

/**
 * @author ithorns
 */
@Slf4j
@RestController
@RequestMapping("/idpt")
@EnableIdempotent
@SpringBootApplication
public class IdempotentApplication {

	public static void main(String[] args) {
		SpringApplication.run(IdempotentApplication.class, args);
	}


	@Resource
	private IdempotentService service;

	@Idempotent
	@PostMapping("/save")
	public String save(@RequestBody Order order) {
		return service.idempotent(order) ? "OK!" : "Failed!";
	}

	@Idempotent
	@GetMapping("/save2")
	public String save2(@RequestParam("code") String code,
						@RequestParam("count") int count,
						@RequestParam("userId") int userId) {
		return service.idempotent(new Order(userId, code, count)) ? "OK!" : "Failed!";
	}

	@Idempotent
	@GetMapping("/save3/{code}/{count}/{userId}")
	public String save3(@PathVariable("code") String code,
						@PathVariable("count") int count,
						@PathVariable("userId") int userId) {
		return service.idempotent(new Order(userId, code, count)) ? "OK!" : "Failed!";
	}

	@GetMapping("/query")
	public String read(){
		return "Read Method";
	}

}